<?php

namespace Web\MainSiteBundle\Controller;

use Base\BaseController;
use Model\CourseBundle\Entity\Course;
use Model\SchoolBundle\Entity\School;
use Model\SchoolBundle\Model\SchoolModel;
use Model\UserBundle\Entity\User;
USE Base\BaseRole;

class WebBaseController extends BaseController
{
    public function showMessagePage($message,$redirect = "")
    {
        //todo 根据网站模板创建消息提示页面
        if (!$message) {
            $message = "javascript:history.go(-1);";
        }
        $data = array(
            "msg"   => $message,
            "url"   => $redirect,
        );
        die( $this->renderView("WebMainSiteBundle::message.html.twig",$data));
    }

    //登录权限检查
    public function chkIsUser()
    {
        $user = $this->getUser();
        if ($user instanceof User) {
            return $user;
        } else {
            return false;
        }
    }
    public function needToBeUser()
    {
        if (!$user = $this->chkIsUser()) {
            $this->jumpToRoute("WebUserLogin");
            return false;//程序不会走到这里的！
        } else {
            return $user;
        }
    }

    //教师权限检查
    public function chkIsTeacher()
    {
        $user = $this->getUser();
        if ($user instanceof User) {
            if (!in_array(BaseRole::LAOSHI,$user->getRoles())) {
                return false;
            }
            return $user;
        } else {
            return false;
        }
    }
    public function needToBeTeacher()
    {
        $message    = "您不是教师，不能执行此操作！";
        $url        = $this->generateUrl("WebUserLogin");
        if (!$user = $this->chkIsTeacher()) {
            $this->showMessagePage($message,$url);
            return false;//其实永远也不会走到这一步，只是为了去掉代码编辑器的黄色警告
        } else {
            return $user;
        }
    }

    //检查课程管理权限
    public function chkIsCourseOwner(Course $course)
    {
        $user = $this->getUser();
        if ($user instanceof User && $user->getId() == $course->getUserId()) {
            return $user;
        } else {
            return false;
        }
    }
    public function chkIsCourseManager(Course $course)
    {
        //todo 关联课程管理员逻辑
        $user = $this->getUser();
        $userRoles = $user->getRoles();
        return in_array(BaseRole::XIAOZHANG,$userRoles);
    }
    public function needToBeCourseManager(Course $course)
    {
        $this->needToBeUser();
        if ($this->chkIsCourseOwner($course) || $this->chkIsCourseManager($course)) {
            return true;
        } else {
            $message    = "您不具备课此程管理权限，不能执行此操作！";
            $url        = "";
            $this->showMessagePage($message,$url);
            return false;
        }
    }

    //学校权限管理
    public function chkIsSchoolManager()
    {
        $user = $this->getUser();
        if ($user instanceof User) {
            if (!in_array( BaseRole::XIAOZHANG , $user->getRoles() )) {
                return false;
            }
            return $user;
        } else {
            return false;
        }
    }

    public function needToBeSchoolManager()
    {
        $message    = "您没有校长权限，不能执行此操作！";
        $url        = $this->generateUrl("WebUserLogin");
        if (!$user = $this->chkIsSchoolManager()) {
            $this->showMessagePage($message,$url);
            return false;//其实永远也不会走到这一步，只是为了去掉代码编辑器的黄色警告
        } else {
            return $user;
        }
    }
    public function chkIsSchoolOwner($schoolOrSchoolId)
    {
        $user = $this->needToBeSchoolManager();
        $schoolId = is_numeric($schoolOrSchoolId) ? $schoolOrSchoolId : $schoolOrSchoolId->getId();
        $schoolModel = new SchoolModel($this->getDoctrine());
        $school = $schoolModel->getById($schoolId);
        if ($school["userId"] == $user->getId()) {
            return $school;
        } else {
            return false;
        }
    }

    public function needToBeSchoolOwner($schoolOrSchoolId)
    {
        $message    = "您不具备此学校的管理权限！";
        $url        = $this->generateUrl("WebUserIndex");
        if (!$school = $this->chkIsSchoolOwner($schoolOrSchoolId)) {
            $this->showMessagePage($message,$url);
            return false;//其实永远也不会走到这一步，只是为了去掉代码编辑器的黄色警告
        } else {
            return $school;
        }
    }

}
